package com.clearance.mine_clearance.entry;

import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @author: houbin
 * @create: 2022-01-10 15:53
 * @description: 统一返回对象
 */
@Builder
@ToString
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final String MSG_QUERY_SUCCESS = "查询成功";
    public static final String MSG_OPERATION_SUCCESS = "操作成功";
    public static final String MSG_OPERATION_FALSE = "操作失败";

    public static final Integer SUCCESS_STATUS = 200;
    public static final Integer FAIL_STATUS = 500;

    /**
     * 状态码 1成功 0失败
     */
    private Integer code;

    /**
     * 错误信息
     */
    private String message;

    /**
     * 数据
     */
    private T data;


    public Result(Throwable e) {
        super();
        this.message = e.getMessage();
    }

    public Result<T> success(T data, String msg) {
        return Result.<T>builder().code(SUCCESS_STATUS).data(data).message(msg).build();
    }

    public Result<T> success(T data) {
        return Result.<T>builder().code(SUCCESS_STATUS).data(data).message(null).build();
    }

    public Result error(String msg) {
        return Result.<T>builder().code(FAIL_STATUS).message(msg).build();
    }

    public Result<T> error(T data, String msg) {
        return Result.<T>builder().code(FAIL_STATUS).data(data).message(msg).build();
    }

    public Result error(Integer code, String msg) {
        return Result.builder().code(code).message(msg).build();
    }

    public Result error(Integer code, String msg, T data) {
        return Result.builder().code(code).message(msg).data(data).build();
    }
}
